package com.ruoyi.project.user.kknumber.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.utils.text.Convert;
import com.ruoyi.framework.aspectj.lang.annotation.DataSource;
import com.ruoyi.framework.aspectj.lang.enums.DataSourceType;
import com.ruoyi.project.poem.poemuser.domain.PoemUser;
import com.ruoyi.project.poem.poemuser.service.IPoemUserService;
import com.ruoyi.project.poem.poemuserinfo.domain.PoemUserInfo;
import com.ruoyi.project.poem.poemuserinfo.service.IPoemUserInfoService;
import com.ruoyi.project.user.dictKkuserInfo.domain.DictKkuserInfo;
import com.ruoyi.project.user.dictKkuserInfo.service.IDictKkuserInfoService;
import com.ruoyi.project.user.kkidmapper.domain.KkidMapper;
import com.ruoyi.project.user.kkidmapper.service.IKkidMapperService;
import com.ruoyi.project.user.kknumber.domain.KkNumber;
import com.ruoyi.project.user.kknumber.mapper.KkNumberMapper;
import com.ruoyi.project.user.kknumber.service.IKkNumberService;
import com.ruoyi.project.user.kkuser.domain.KKUser;
import com.ruoyi.project.user.kkuser.service.IKKUserService;
import com.ruoyi.project.user.platformKkuserInfo.domain.PlatformKkuserInfo;
import com.ruoyi.project.user.platformKkuserInfo.service.IPlatformKkuserInfoService;
import com.ruoyi.project.user.usermapper.domain.UserMapper;
import com.ruoyi.project.user.usermapper.service.IUserMapperService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * K号查询Service业务层处理
 *
 * @author shixiangwen
 * @date 2022-10-11
 */
@Service
@DataSource(DataSourceType.ACCOUNT)
public class KkNumberServiceImpl implements IKkNumberService {

    @Autowired
    private KkNumberMapper kkNumberMapper;

    @Autowired
    private IDictKkuserInfoService dictKkuserInfoService;

    @Autowired
    private IKKUserService ikkUserService;

    @Autowired
    private IKkidMapperService kkidMapperService;

    @Autowired
    private IUserMapperService userMapperService;

    @Autowired
    private IPoemUserService poemUserService;

    @Autowired
    private IPoemUserInfoService poemUserInfoService;

    @Autowired
    private IPlatformKkuserInfoService platformKkuserInfoService;

    /**
     * 查询K号查询
     *
     * @param id K号查询ID
     * @return K号查询
     */
    @Override
    public KkNumber selectKkNumberById(Long id) {
        return kkNumberMapper.selectKkNumberById(id);
    }

    /**
     * 查询K号查询列表
     *
     * @param query K号查询
     * @return K号查询
     */
    @Override
    public List<KkNumber> selectKkNumberList(KkNumber query) {

        if (query.getKkid() == null && query.getNo() == null) {
            return new ArrayList<>();
        }

        List<KkNumber> kkNumbers = kkNumberMapper.selectKkNumberList(query);
        long kkuid = 0;
        for (KkNumber kkNumber : kkNumbers) {
            String thirdType = "";
            if (StringUtils.equals(kkNumber.getAppId(), "kkdict") || StringUtils.equals(kkNumber.getAppId(), "kkpoem")) {
                KkidMapper kkidMapperQuery = new KkidMapper();
                kkidMapperQuery.setAppId(kkNumber.getAppId());
                kkidMapperQuery.setKkid(kkNumber.getKkid());
                KkidMapper kkidMapper = kkidMapperService.queryKkidMapper(kkidMapperQuery);
                if (kkidMapper == null) {
                    continue;
                }
                if (StringUtils.equals(kkNumber.getAppId(), "kkdict")) {
                    DictKkuserInfo dictKkuserInfo = dictKkuserInfoService.selectDictKkuserInfoById(Long.parseLong(kkidMapper.getUid()));
                    if (dictKkuserInfo != null) {
                        kkNumber.setNickname(dictKkuserInfo.getNickname());
                        kkNumber.setProtrait(dictKkuserInfo.getProtrait());
                        if (dictKkuserInfo.getType() == 1) {
                            thirdType = "qq";
                        } else if (dictKkuserInfo.getType() == 2) {
                            thirdType = "weixin";
                        } else if (dictKkuserInfo.getType() == 4) {
                            thirdType = "kk";
                        }
                        kkNumber.setThirdType(thirdType);
                        kkNumber.setRegTime(dictKkuserInfo.getRegistertime());
                    }
                    if (dictKkuserInfo.getType() == 4) {
                        kkuid = Long.parseLong(dictKkuserInfo.getOpenid());
                    }
                } else {
                    PoemUser poemUser = poemUserService.selectPoemUserOne(kkidMapper.getUid());
                    if (poemUser != null) {
                        PoemUserInfo poemUserInfo = poemUserInfoService.selectPoemUserInfoOne(kkidMapper.getUid());
                        if (poemUserInfo != null) {
                            kkNumber.setProtrait(Constants.POEMBBS_CDN_URL + poemUserInfo.getPortrait());
                            kkNumber.setNickname(poemUserInfo.getNickname());
                        } else {
                            kkNumber.setProtrait(Constants.POEMBBS_CDN_URL + poemUser.getPortrait());
                            kkNumber.setNickname(poemUser.getNickname());
                        }
                        kkNumber.setThirdType(poemUser.getThirdType());
                        kkNumber.setRegTime(poemUser.getCreatedTime());
                        if (StringUtils.equals(poemUser.getThirdType(), "kk")) {
                            kkuid = Long.parseLong(poemUser.getThirdUserId());
                        }
                    }
                }
            } else {
                KKUser kkUserQuery = new KKUser();
                kkUserQuery.setKkid(kkNumber.getKkid());
                kkUserQuery.setAppId(kkNumber.getAppId());
                List<KKUser> kkUsers = ikkUserService.selectKKUserList(kkUserQuery);
                if (kkUsers.isEmpty()) {
                    continue;
                }
                KKUser kkUser = kkUsers.get(0);
                kkNumber.setProtrait(kkUser.getPortrait());
                kkNumber.setRegTime(kkUser.getCreatedTime());
                kkNumber.setNickname(kkUser.getNickname());

                UserMapper userMapperQuery = new UserMapper();
                userMapperQuery.setKkid(kkNumber.getKkid());
                userMapperQuery.setAppId(kkNumber.getAppId());
                List<UserMapper> userMappers = userMapperService.selectUserMapperList(userMapperQuery);
                if (!userMappers.isEmpty()) {
                    thirdType = userMappers.get(0).getThirdType();
                    kkNumber.setThirdType(thirdType);
                    if (StringUtils.equals(thirdType, "kk")) {
                        kkuid = Long.parseLong(userMappers.get(0).getThirdUserId());
                    }
                }
            }

            if (StringUtils.equals(thirdType, "kk")) {
                PlatformKkuserInfo platformKkuserInfo = platformKkuserInfoService.selectPlatformKkuserInfoById(kkuid);
                if (platformKkuserInfo != null) {
                    kkNumber.setPhone(org.apache.commons.lang3.StringUtils.overlay(String.valueOf(platformKkuserInfo.getPhone()), "****", 3, 7));
                }
            }

        }
        return kkNumbers;
    }

    /**
     * 新增K号查询
     *
     * @param kkNumber K号查询
     * @return 结果
     */
    @Override
    public int insertKkNumber(KkNumber kkNumber) {
        return kkNumberMapper.insertKkNumber(kkNumber);
    }

    /**
     * 修改K号查询
     *
     * @param kkNumber K号查询
     * @return 结果
     */
    @Override
    public int updateKkNumber(KkNumber kkNumber) {
        return kkNumberMapper.updateKkNumber(kkNumber);
    }

    /**
     * 删除K号查询对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteKkNumberByIds(String ids) {
        return kkNumberMapper.deleteKkNumberByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除K号查询信息
     *
     * @param id K号查询ID
     * @return 结果
     */
    @Override
    public int deleteKkNumberById(Long id) {
        return kkNumberMapper.deleteKkNumberById(id);
    }
}
